home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_014 / amiga3d / threed.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  28KB  |  1,087 lines

  1. #include <exec/types.h>
  2. #include <exec/nodes.h>
  3. #include <exec/lists.h>
  4. #include <exec/memory.h>
  5. #include <hardware/blit.h>
  6. #include <hardware/custom.h>
  7. #include <graphics/gfx.h>
  8. #include <graphics/gfxbase.h>
  9. #include <graphics/clip.h>
  10. #include <graphics/rastport.h>
  11. #include <graphics/view.h>
  12. #include <graphics/text.h>
  13. #include <graphics/gfxmacros.h>
  14.  
  15. #include <graphics/layers.h>
  16. #include <intuition/intuition.h>
  17. #include <libraries/dos.h>
  18. #include "threed.h"
  19. #include "joystick.h"
  20.  
  21. /* #define DEBUG */
  22. /* #define ODEBUG */
  23. /* #define DRAWDEBUG */
  24. /* #define TICKDEBUG */
  25. /* #define TICKPRINT */
  26. /* #define JOYDEBUG */
  27. /* #define OLDALLOCATE */
  28. /* #define OLDCAMERA */
  29. #define FIXEDILLUMINATION
  30. #define CYCLECOLORS
  31.  
  32. #define NUM_OF_PATTERNS 2
  33.  
  34. #define IS_PAL (((struct GfxBase *)GfxBase)->DisplayFlags & PAL)
  35.  
  36. UWORD patterns[NUM_OF_PATTERNS][8] =
  37. {
  38.    0,0,0,0,0,0,0,0,       /* solid penb */
  39.    -1,-1,-1,-1,-1,-1,-1,-1,   /* solid pena */
  40. };
  41.  
  42. #ifndef FIXEDILLUMINATION
  43.  
  44. UWORD colorpalette[MAXNUMCOLORS] =
  45. {
  46.     0x0000,
  47.     0x0222,
  48.     0x0444,
  49.     0x0666,
  50.     0x0888,
  51.     0x0aaa,
  52.     0x0ccc,
  53.     0x0fff,
  54.     0x0000,
  55.     0x0222,
  56.     0x0444,
  57.     0x0666,
  58.     0x0888,
  59.     0x0aaa,
  60.     0x0ccc,
  61.     0x0fff,
  62.     0x0000,
  63.     0x0222,
  64.     0x0444,
  65.     0x0666,
  66.     0x0888,
  67.     0x0aaa,
  68.     0x0ccc,
  69.     0x0fff,
  70.     0x0000,
  71.     0x0222,
  72.     0x0444,
  73.     0x0666,
  74.     0x0888,
  75.     0x0aaa,
  76.     0x0ccc,
  77.     0x0fff,
  78. };
  79.  
  80. #else
  81.  
  82. UWORD colorpalette[MAXNUMCOLORS] =
  83. {
  84.     0x0456,
  85.     0x0006,
  86.     0x0009,
  87.     0x000c,
  88.     0x000f,
  89.     0x0004,
  90.     0x0f00,
  91.     0x0fff,
  92.     0x0888,
  93.     0x0999,
  94.     0x0aaa,
  95.     0x0bbb,
  96.     0x0ccc,
  97.     0x0ddd,
  98.     0x0eee,
  99.     0x0fff,
  100.     0x0000,
  101.     0x000f,
  102.     0x000c,
  103.     0x0008,
  104.     0x000c,
  105.     0x0004,
  106.     0x0f00,
  107.     0x0fff,
  108.     0x0888,
  109.     0x0999,
  110.     0x0aaa,
  111.     0x0bbb,
  112.     0x0ccc,
  113.     0x0ddd,
  114.     0x0eee,
  115.     0x0fff
  116. };
  117.  
  118. #endif
  119.  
  120. UBYTE title[] = "3D WINDOW";
  121.  
  122. extern struct Custom custom;
  123.  
  124. struct TmpRas tmpras;
  125.  
  126. struct BitMap bitmap0;
  127. struct BitMap bitmap1;
  128.  
  129. struct RastPort r[2];
  130. struct RastPort *rp[2];
  131.  
  132. struct RasInfo ri[2];
  133. struct RasInfo *rip[2];
  134.  
  135. struct RasInfo *irip;
  136.  
  137. WORD pcount = 0;
  138. WORD vcount = 0;
  139.  
  140. UWORD frametoggle = 0;
  141.  
  142. struct Objectinfo *universeobjectinfo = NULL;
  143. struct Objectinfo *cameraobjectinfo = NULL;
  144.  
  145. BPTR objectsegment = NULL;
  146.  
  147. struct Object *Amiga = NULL;
  148.  
  149. struct UV *cameramatrix = NULL;
  150. struct Coordinate *cameraposition = NULL;
  151.  
  152. extern struct Object Camera1;
  153.  
  154. extern struct UV camera1matrix;
  155. extern struct Coordinate camera1position;
  156. extern int camera1procedure();
  157.  
  158. extern long GfxBase;
  159. extern long DosBase;
  160.  
  161. extern struct Object *UniverseObject;
  162.  
  163. /******************************************************************************/
  164.  
  165. do3d(view,screen,window,state)
  166. struct View *view;
  167. struct Screen *screen;
  168. struct Window *window;
  169. ULONG state;
  170. {
  171.     int error = FALSE;
  172.  
  173.     /* copy rastport from *(window->RPort) to r[0], r[1] */
  174.  
  175.     r[0] = *(window->RPort);
  176.     r[1] = *(window->RPort);
  177.  
  178.     /* force non-layer interperetation of r[0,1] */
  179.  
  180.     /* r[0].Layer = 0; */
  181.     /* r[1].Layer = 0; */
  182.  
  183.     /* set NOCROSSFILL flag in r[0,1] */
  184.  
  185.     r[0].Flags |= NOCROSSFILL;
  186.     r[1].Flags |= NOCROSSFILL;
  187.  
  188.     /* point r[0,1] at tmpras */
  189.  
  190.     r[0].TmpRas = &tmpras;
  191.     r[1].TmpRas = &tmpras;
  192.  
  193.     /* point r[0] at bitmap0, r[1] at bitmap1 */
  194.  
  195.     r[0].BitMap = &bitmap0;
  196.     r[1].BitMap = &bitmap1;
  197.  
  198.     /* copy rasinfo from *((&screen->ViewPort)->Rasinfo) to ri[0], ri[1] */ 
  199.  
  200.     ri[0] = *((&screen->ViewPort)->RasInfo);
  201.     ri[1] = *((&screen->ViewPort)->RasInfo);
  202.  
  203.     /* point ri[0] at bitmap0, ri[1] at bitmap1 */
  204.  
  205.     ri[0].BitMap = &bitmap0;
  206.     ri[1].BitMap = &bitmap1;
  207.  
  208.     /* clear the raster that we're NOT displaying */
  209.  
  210.     SetRast(rp[frametoggle ^ 0x0001],0);
  211.  
  212. #ifdef OLDCAMERA
  213.  
  214.     /* rotate the camera matrix */
  215.     {
  216.  
  217.    if (!(state & BUTTON_DOWN))
  218.    {
  219.        if (state & BUTTON_FORWARD)
  220.        {
  221.       struct Coordinate cfb;
  222.  
  223.       cfb.x = (cameraobjectinfo->objectmatrix->uv31 < 0)?(cameraobjectinfo->objectmatrix->uv31)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv31)>>8 ;
  224.       cfb.y = (cameraobjectinfo->objectmatrix->uv32 < 0)?(cameraobjectinfo->objectmatrix->uv32)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv32)>>8 ;
  225.       cfb.z = (cameraobjectinfo->objectmatrix->uv33 < 0)?(cameraobjectinfo->objectmatrix->uv33)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv33)>>8 ;
  226.  
  227.       addvect(&cfb,cameraposition,cameraposition);
  228.        }
  229.  
  230.        if (state & BUTTON_BACK )
  231.        {
  232.       struct Coordinate cfb;
  233.  
  234.       cfb.x = (cameraobjectinfo->objectmatrix->uv31 < 0)?(cameraobjectinfo->objectmatrix->uv31)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv31)>>8 ;
  235.       cfb.y = (cameraobjectinfo->objectmatrix->uv32 < 0)?(cameraobjectinfo->objectmatrix->uv32)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv32)>>8 ;
  236.       cfb.z = (cameraobjectinfo->objectmatrix->uv33 < 0)?(cameraobjectinfo->objectmatrix->uv33)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv33)>>8 ;
  237.  
  238.       subvect(&cfb,cameraposition,cameraposition);
  239.        }
  240.  
  241.        if (state & BUTTON_RIGHT)
  242.        {
  243.       struct Coordinate crl;
  244.  
  245.       crl.x = (cameraobjectinfo->objectmatrix->uv11 < 0)?(cameraobjectinfo->objectmatrix->uv11)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv11)>>8 ;
  246.       crl.y = (cameraobjectinfo->objectmatrix->uv12 < 0)?(cameraobjectinfo->objectmatrix->uv12)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv12)>>8 ;
  247.       crl.z = (cameraobjectinfo->objectmatrix->uv13 < 0)?(cameraobjectinfo->objectmatrix->uv13)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv13)>>8 ;
  248.  
  249.       addvect(&crl,cameraposition,cameraposition);
  250.        }
  251.  
  252.        if (state &  BUTTON_LEFT)
  253.        {
  254.       struct Coordinate crl;
  255.  
  256.       crl.x = (cameraobjectinfo->objectmatrix->uv11 < 0)?(cameraobjectinfo->objectmatrix->uv11)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv11)>>8 ;
  257.       crl.y = (cameraobjectinfo->objectmatrix->uv12 < 0)?(cameraobjectinfo->objectmatrix->uv12)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv12)>>8 ;
  258.       crl.z = (cameraobjectinfo->objectmatrix->uv13 < 0)?(cameraobjectinfo->objectmatrix->uv13)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv13)>>8 ;
  259.  
  260.       subvect(&crl,cameraposition,cameraposition);
  261.        }
  262.  
  263.  
  264.    }
  265.    else
  266.    {
  267.        if (state & BUTTON_FORWARD)
  268.        {
  269.       struct Coordinate cud;
  270.  
  271.       cud.x = (cameraobjectinfo->objectmatrix->uv21 < 0)?(cameraobjectinfo->objectmatrix->uv21)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv21)>>8 ;
  272.       cud.y = (cameraobjectinfo->objectmatrix->uv22 < 0)?(cameraobjectinfo->objectmatrix->uv22)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv22)>>8 ;
  273.       cud.z = (cameraobjectinfo->objectmatrix->uv23 < 0)?(cameraobjectinfo->objectmatrix->uv23)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv23)>>8 ;
  274.  
  275.       subvect(&cud,cameraposition,cameraposition);
  276.        }
  277.  
  278.        if (state & BUTTON_BACK )
  279.        {
  280.       struct Coordinate cud;
  281.  
  282.       cud.x = (cameraobjectinfo->objectmatrix->uv21 < 0)?(cameraobjectinfo->objectmatrix->uv21)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv21)>>8 ;
  283.       cud.y = (cameraobjectinfo->objectmatrix->uv22 < 0)?(cameraobjectinfo->objectmatrix->uv22)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv22)>>8 ;
  284.       cud.z = (cameraobjectinfo->objectmatrix->uv23 < 0)?(cameraobjectinfo->objectmatrix->uv23)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv23)>>8 ;
  285.  
  286.       addvect(&cud,cameraposition,cameraposition);
  287.        }
  288.  
  289.        if (state & BUTTON_RIGHT)   yaw(cameraobjectinfo->objectmatrix,-SINB,COSB);
  290.        if (state & BUTTON_LEFT )   yaw(cameraobjectinfo->objectmatrix, SINB,COSB);
  291.    }
  292.  
  293.     }
  294.  
  295. #else
  296.  
  297.     /* rotate the camera matrix */
  298.     {
  299.  
  300.    if (!(state & BUTTON_DOWN))
  301.    {
  302.        if (state & BUTTON_FORWARD)
  303.        {
  304.       struct Coordinate cfb;
  305.  
  306.       cfb.x = (cameraobjectinfo->objectmatrix->uv31 < 0)?(cameraobjectinfo->objectmatrix->uv31)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv31)>>8 ;
  307.       cfb.y = (cameraobjectinfo->objectmatrix->uv32 < 0)?(cameraobjectinfo->objectmatrix->uv32)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv32)>>8 ;
  308.       cfb.z = (cameraobjectinfo->objectmatrix->uv33 < 0)?(cameraobjectinfo->objectmatrix->uv33)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv33)>>8 ;
  309.  
  310.       addvect(&cfb,cameraobjectinfo->objectposition,cameraobjectinfo->objectposition);
  311.       /* keep amiga behind the screen */
  312.  
  313.       if (cameraobjectinfo->objectposition->z > -1536) cameraobjectinfo->objectposition->z = -1536;
  314.  
  315.       /* keep amiga behind the screen */
  316.        }
  317.  
  318.        if (state & BUTTON_BACK )
  319.        {
  320.       struct Coordinate cfb;
  321.  
  322.       cfb.x = (cameraobjectinfo->objectmatrix->uv31 < 0)?(cameraobjectinfo->objectmatrix->uv31)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv31)>>8 ;
  323.       cfb.y = (cameraobjectinfo->objectmatrix->uv32 < 0)?(cameraobjectinfo->objectmatrix->uv32)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv32)>>8 ;
  324.       cfb.z = (cameraobjectinfo->objectmatrix->uv33 < 0)?(cameraobjectinfo->objectmatrix->uv33)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv33)>>8 ;
  325.  
  326.       subvect(&cfb,cameraobjectinfo->objectposition,cameraobjectinfo->objectposition);
  327.        }
  328.  
  329.        if (state & BUTTON_RIGHT)
  330.        {
  331.       struct Coordinate crl;
  332.  
  333.       crl.x = (cameraobjectinfo->objectmatrix->uv11 < 0)?(cameraobjectinfo->objectmatrix->uv11)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv11)>>8 ;
  334.       crl.y = (cameraobjectinfo->objectmatrix->uv12 < 0)?(cameraobjectinfo->objectmatrix->uv12)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv12)>>8 ;
  335.       crl.z = (cameraobjectinfo->objectmatrix->uv13 < 0)?(cameraobjectinfo->objectmatrix->uv13)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv13)>>8 ;
  336.  
  337.       addvect(&crl,cameraobjectinfo->objectposition,cameraobjectinfo->objectposition);
  338.        }
  339.  
  340.        if (state &  BUTTON_LEFT)
  341.        {
  342.       struct Coordinate crl;
  343.  
  344.       crl.x = (cameraobjectinfo->objectmatrix->uv11 < 0)?(cameraobjectinfo->objectmatrix->uv11)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv11)>>8 ;
  345.       crl.y = (cameraobjectinfo->objectmatrix->uv12 < 0)?(cameraobjectinfo->objectmatrix->uv12)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv12)>>8 ;
  346.       crl.z = (cameraobjectinfo->objectmatrix->uv13 < 0)?(cameraobjectinfo->objectmatrix->uv13)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv13)>>8 ;
  347.  
  348.       subvect(&crl,cameraobjectinfo->objectposition,cameraobjectinfo->objectposition);
  349.        }
  350.  
  351.  
  352.    }
  353.    else
  354.    {
  355.  
  356.        if (state & BUTTON_FORWARD)
  357.        {
  358.       struct Coordinate cud;
  359.  
  360.       cud.x = (cameraobjectinfo->objectmatrix->uv21 < 0)?(cameraobjectinfo->objectmatrix->uv21)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv21)>>8 ;
  361.       cud.y = (cameraobjectinfo->objectmatrix->uv22 < 0)?(cameraobjectinfo->objectmatrix->uv22)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv22)>>8 ;
  362.       cud.z = (cameraobjectinfo->objectmatrix->uv23 < 0)?(cameraobjectinfo->objectmatrix->uv23)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv23)>>8 ;
  363.  
  364.       addvect(&cud,cameraobjectinfo->objectposition,cameraobjectinfo->objectposition);
  365.        }
  366.  
  367.        if (state & BUTTON_BACK )
  368.        {
  369.       struct Coordinate cud;
  370.  
  371.       cud.x = (cameraobjectinfo->objectmatrix->uv21 < 0)?(cameraobjectinfo->objectmatrix->uv21)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv21)>>8 ;
  372.       cud.y = (cameraobjectinfo->objectmatrix->uv22 < 0)?(cameraobjectinfo->objectmatrix->uv22)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv22)>>8 ;
  373.       cud.z = (cameraobjectinfo->objectmatrix->uv23 < 0)?(cameraobjectinfo->objectmatrix->uv23)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv23)>>8 ;
  374.  
  375.       subvect(&cud,cameraobjectinfo->objectposition,cameraobjectinfo->objectposition);
  376.        }
  377.  
  378.        if (state & BUTTON_RIGHT)
  379.        {
  380.       struct Coordinate crl;
  381.  
  382.       crl.x = (cameraobjectinfo->objectmatrix->uv11 < 0)?(cameraobjectinfo->objectmatrix->uv11)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv11)>>8 ;
  383.       crl.y = (cameraobjectinfo->objectmatrix->uv12 < 0)?(cameraobjectinfo->objectmatrix->uv12)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv12)>>8 ;
  384.       crl.z = (cameraobjectinfo->objectmatrix->uv13 < 0)?(cameraobjectinfo->objectmatrix->uv13)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv13)>>8 ;
  385.  
  386.       addvect(&crl,cameraobjectinfo->objectposition,cameraobjectinfo->objectposition);
  387.        }
  388.  
  389.        if (state &  BUTTON_LEFT)
  390.        {
  391.       struct Coordinate crl;
  392.  
  393.       crl.x = (cameraobjectinfo->objectmatrix->uv11 < 0)?(cameraobjectinfo->objectmatrix->uv11)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv11)>>8 ;
  394.       crl.y = (cameraobjectinfo->objectmatrix->uv12 < 0)?(cameraobjectinfo->objectmatrix->uv12)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv12)>>8 ;
  395.       crl.z = (cameraobjectinfo->objectmatrix->uv13 < 0)?(cameraobjectinfo->objectmatrix->uv13)>>8|0xFF00:(cameraobjectinfo->objectmatrix->uv13)>>8 ;
  396.  
  397.       subvect(&crl,cameraobjectinfo->objectposition,cameraobjectinfo->objectposition);
  398.        }
  399.  
  400.        /*
  401.  
  402.        if (state & BUTTON_RIGHT)   yaw(cameraobjectinfo->objectmatrix,-SINB,COSB);
  403.        if (state & BUTTON_LEFT )   yaw(cameraobjectinfo->objectmatrix, SINB,COSB);
  404.        */
  405.    }
  406.  
  407.     }
  408.  
  409. #endif
  410.  
  411.     /* draw all the objects in the list headed by firsobjectinfo */
  412.  
  413. #ifdef OLDCAMERA
  414.  
  415. #ifdef DISPLAYDEBUG
  416.     printf("do3d: call display(%lx)...\n",universeobjectinfo);
  417. #endif
  418.  
  419.     display(view,screen,window,universeobjectinfo);
  420.  
  421. #ifdef DISPLAYDEBUG
  422.     printf("do3d: returned from display(%lx)...\n",universeobjectinfo);
  423. #endif
  424.  
  425. #else
  426.  
  427. #ifdef DISPLAYDEBUG
  428.     printf("do3d: call displayuniverse(%lx)...\n",universeobjectinfo);
  429. #endif
  430.  
  431.     displayuniverse(view,screen,window,cameraobjectinfo,universeobjectinfo);
  432.  
  433. #ifdef DISPLAYDEBUG
  434.     printf("do3d: returned from displayuniverse(%lx)...\n",universeobjectinfo);
  435. #endif
  436.  
  437. #endif
  438.  
  439.     /* for double buffering */
  440.  
  441.     frametoggle = frametoggle ^ 0x0001;
  442.  
  443.     return(error);
  444.  
  445. }
  446.  
  447. toggle(onoff)
  448. WORD *onoff;
  449. {
  450.     switch (*onoff)
  451.     {
  452.    case OFF:   *onoff = ON;
  453.          break;
  454.  
  455.    case ON:   *onoff = OFF;
  456.          break;
  457.  
  458.    default:   break;
  459.     }
  460.  
  461. }
  462.  
  463. test3d(view,screen,window)
  464. struct View *view;
  465. struct Screen *screen;
  466. struct Window *window;
  467. {
  468.     int tickcount = 0;
  469.     int error = FALSE;
  470.     WORD actiontoggle = OFF;
  471.     struct IntuiMessage *message;
  472.     WORD mousex, mousey;
  473.     WORD i, j, k;
  474.     WORD mousetoggle = UP ;
  475.     WORD messagenum = 0;
  476.     ULONG framecount = 0;
  477.     ULONG state = 0;
  478.     struct IOStdReq *ior;
  479.  
  480.  
  481. #ifdef DEBUG
  482.     printf("test3d: entering test3d...\n");
  483. #endif
  484.  
  485.     /* open the gameport device for joystick input */
  486.  
  487.     ior = open_joystick();
  488.  
  489.     /* set colors for this screen */
  490.  
  491.     for(i = 0; i < (1<<N_BIT_PLANES); i++)
  492.     {
  493.    SetRGB4(&screen->ViewPort,i,(colorpalette[i]>>8)&0xF,
  494.                 (colorpalette[i]>>4)&0xF,(colorpalette[i])&0xF);
  495.     }
  496.  
  497.     /* copy rastport from *(window->RPort) to r[0], r[1] */
  498.  
  499.     r[0] = *(window->RPort);
  500.     r[1] = *(window->RPort);
  501.  
  502.     /* force non-layer interperetation of r[0,1] */
  503.  
  504.     /* r[0].Layer = 0; */
  505.     /* r[1].Layer = 0; */
  506.  
  507.     /* set NOCROSSFILL flag in r[0,1] */
  508.  
  509.     r[0].Flags |= NOCROSSFILL;
  510.     r[1].Flags |= NOCROSSFILL;
  511.  
  512.     /* point r[0,1] at tmpras */
  513.  
  514.     r[0].TmpRas = &tmpras;
  515.     r[1].TmpRas = &tmpras;
  516.  
  517.     /* point rp[0] at r[0], rp[1] at r[1] */
  518.  
  519.     rp[0] = &r[0];
  520.     rp[1] = &r[1];
  521.  
  522.     /* point r[0] at bitmap0, r[1] at bitmap1 */
  523.  
  524.     r[0].BitMap = &bitmap0;
  525.     r[1].BitMap = &bitmap1;
  526.  
  527.     /* clone rasinfo pointer from (&screen->ViewPort->RasInfo) to irip */
  528.  
  529.     irip = (&screen->ViewPort)->RasInfo;
  530.  
  531.     /* copy rasinfo from *((&screen->ViewPort)->RasInfo) to ri[0], ri[1] */ 
  532.  
  533.     ri[0] = *((&screen->ViewPort)->RasInfo);
  534.     ri[1] = *((&screen->ViewPort)->RasInfo);
  535.  
  536.     /* point ri[1] at bitmap1 */
  537.     /* don't point r1[0] at bitmap0 yet, so that we can clear initial rastport bitmap */
  538.  
  539.     ri[1].BitMap = &bitmap1;
  540.  
  541.     /* point rip[0] at ri[0], rip[1] at ri[1] */
  542.  
  543.     rip[0] = &ri[0];
  544.     rip[1] = &ri[1];
  545.  
  546.     /* clear the raster that we ARE displaying */
  547.  
  548.     SetRast(rp[frametoggle],0);
  549.  
  550.     /* reset the frametoggle */
  551.  
  552.     frametoggle = 0;
  553.  
  554. #ifdef OLDCAMERA
  555.  
  556.     /* allocate a cameramatrix for this display */
  557.  
  558.     if ((cameramatrix = (struct UV *)AllocMem(sizeof(struct UV),MEMF_PUBLIC)) == NULL) 
  559.     {
  560.    return(error); 
  561.     }
  562.  
  563.     /* initialize the cameramatrix */
  564.  
  565.     matrixinit(cameramatrix);
  566.  
  567.     /* allocate a cameraposition for this display */
  568.  
  569.     if ((cameraposition = (struct Coordinate *)AllocMem(sizeof(struct Coordinate),MEMF_PUBLIC)) == NULL) 
  570.     {
  571.    return(error); 
  572.     }
  573.  
  574.     /* initialize the cameraposition */
  575.  
  576.     positioninit(cameraposition);
  577.     cameraposition->z = 0xfa00;
  578.  
  579. #else
  580.  
  581.     /* allocate a cameraobject for this display */
  582.  
  583.     allocateobjectinfolist(view,screen,window,&cameraobjectinfo,&Camera1);
  584.  
  585.     /* initialize the cameramatrix */
  586.  
  587.     matrixinit(cameraobjectinfo->objectmatrix);
  588.  
  589.     /* initialize the cameraposition */
  590.  
  591.     positioninit(cameraobjectinfo->objectposition);
  592.  
  593.     cameraobjectinfo->objectposition->z = 0xfa00;
  594.  
  595. #endif
  596.  
  597.     /* allocate objectinfo structures */
  598.  
  599. #ifdef OLDALLOCATE
  600.     {
  601.    struct Object *ob;
  602.  
  603.    /* for all the objects in this objectsegment */
  604.  
  605.    for (ob = Amiga; ob; ob = ob->nextobject)
  606.    {
  607.        struct Objectinfo *thisobjectinfo;
  608.  
  609. #ifdef ODEBUG
  610.        printf("test3d: allocate objectinfo for object(%lx) ",ob);
  611. #endif
  612.  
  613.        /* allocate an objectinfo structure for the current object */
  614.  
  615.        if ((thisobjectinfo = (struct Objectinfo *)AllocMem(sizeof(struct Objectinfo),MEMF_PUBLIC|MEMF_CLEAR)) == NULL) 
  616.        {
  617.       return(error); 
  618.        }
  619.  
  620. #ifdef ODEBUG
  621.        printf("= %lx\n",thisobjectinfo);
  622. #endif
  623.        /* initialize the buffers for the current 3d object */
  624.  
  625.        if(!objectinit(view,screen,window,thisobjectinfo,ob))
  626.        {
  627.           return(error);
  628.        }
  629.  
  630.        /* make this objectinfo last on the objectinfo list */
  631.        {
  632.       struct Objectinfo **oipp;
  633.  
  634.       oipp =  &universeobjectinfo;
  635.       while (*oipp)
  636.       {
  637.           oipp = &((*oipp)->nextobjectinfo);
  638.       }
  639.       *oipp = thisobjectinfo;
  640.        thisobjectinfo->nextobjectinfo = NULL;
  641.        }
  642.  
  643.    }
  644.  
  645.     }
  646. #else
  647.     
  648.     allocateobjectinfolist(view,screen,window,&universeobjectinfo,Amiga);
  649.  
  650. #endif
  651.  
  652. #ifdef ODEBUG
  653.     printf("test3d: &universeobjectinfo = %lx\n",&universeobjectinfo);
  654.     {
  655.    struct Objectinfo *oip;
  656.  
  657.    printf("    OBJECTINFO LIST     \n");
  658.    printf("_________________________\n");
  659.  
  660.    for (oip = universeobjectinfo; oip; oip = oip->nextobjectinfo)
  661.           printf("    objectinfo(%lx)\n",oip);
  662.  
  663.    printf("_________________________\n");
  664.     }
  665. #endif
  666.  
  667.     /* now wait for instructions from the user */
  668.  
  669.     message = (struct IntuiMessage *)GetMsg(window->UserPort);
  670.  
  671.     while ( (!message) || (message->Class) != CLOSEWINDOW )
  672.     {
  673.    if(message) ReplyMsg(message);
  674.  
  675.    /* read joystick, if any */
  676.  
  677.    if (ior) state = test_joystick(ior,state);
  678.  
  679. #ifdef JOYDEBUG
  680.    printf("do3d: state = %lx\n",state);
  681. #endif
  682.  
  683.    /* call do3d */
  684.  
  685. #ifdef ODEBUG
  686.    printf("test3d: call do3d...\n");
  687. #endif
  688.  
  689. #ifdef TICKDEBUG
  690.  
  691.    /* through timer */
  692.  
  693.    tickcount = timeproc(do3d,view,screen,window,state);
  694.  
  695. #ifdef TICKPRINT
  696.    printf("%lx\n",tickcount);
  697. #endif
  698.  
  699. #else
  700.    error = do3d(view,screen,window,state);
  701.  
  702. #ifdef ODEBUG
  703.    printf("test3d: returned from call to do3d... error = %lx\n",error);
  704. #endif
  705.  
  706. #endif
  707.  
  708.    /* did 3d, increment framecount */
  709.  
  710.    framecount += 17;
  711.  
  712. #ifdef CYCLECOLORS
  713.  
  714.    /* cycle colors for AMIGA polygons */
  715.  
  716.    SetRGB4(&screen->ViewPort,6,((framecount & 0x00000fff)>>8 )&0xF,
  717.                 ((framecount & 0x00000fff)>>4 )&0xF,
  718.                 ((framecount & 0x00000fff)    )&0xF);
  719.    SetRGB4(&screen->ViewPort,7,((framecount & 0x00000f00)>>10)+0xB,
  720.                 ((framecount & 0x000000f0)>>6 )+0xB,
  721.                 ((framecount & 0x0000000f)>>2 )+0xB);
  722.  
  723. #endif
  724.  
  725.    /* double buffer */
  726.  
  727.    Forbid();
  728.    WaitTOF();
  729.    WaitBlit();
  730.    Disable();
  731.    (&screen->ViewPort)->RasInfo = rip[frametoggle];
  732.    ScrollVPort(&screen->ViewPort);
  733.    Enable();
  734.    Permit();
  735.  
  736.    message = (struct IntuiMessage *)GetMsg(window->UserPort);
  737.     }
  738.     ReplyMsg(message);
  739.  
  740.     /* deallocate objectinfo structures */
  741.  
  742. #ifdef ODEBUG
  743.     printf("test3d: deallocate the active objectinfo structures...\n");
  744. #endif
  745.  
  746. #ifdef OLDALLOCATE
  747.     while( universeobjectinfo )
  748.     {
  749.    struct Objectinfo *oip;
  750.  
  751.    /* delink the first objectinfo from the objectinfo list */
  752.  
  753.    oip = universeobjectinfo;
  754.  
  755.    universeobjectinfo = universeobjectinfo->nextobjectinfo;
  756.  
  757.    /* deallocate the buffers dependent on the current objectinfo */
  758.  
  759. #ifdef ODEBUG
  760.     printf("    deallocate objectinfo(%lx)\n",oip);
  761. #endif
  762.  
  763.    objectdeallocate(view,screen,window,oip);
  764.  
  765.    /* deallocate this objectinfo structure itself */
  766.  
  767.    FreeMem(oip,sizeof(struct Objectinfo));
  768.     }
  769. #else
  770.  
  771.     deallocateobjectinfolist(view,screen,window,&universeobjectinfo);
  772.  
  773. #endif
  774.  
  775. #ifdef OLDCAMERA
  776.  
  777.     /* free camera allocations */
  778.  
  779.     if(cameramatrix)
  780.       FreeMem(cameramatrix,sizeof(struct UV));
  781.  
  782.     if(cameraposition)
  783.       FreeMem(cameraposition,sizeof(struct Coordinate));
  784.  
  785. #else
  786.  
  787.     deallocateobjectinfolist(view,screen,window,&cameraobjectinfo);
  788.  
  789. #endif
  790.  
  791.     /* if joystick, close device */
  792.  
  793.     if (ior) close_joystick(ior);
  794.  
  795.     /* restore rasinfo pointer from irip to (view->ViewPort->RasInfo) */
  796.  
  797.     view->ViewPort->RasInfo = irip;
  798.  
  799.     return(error);
  800.  
  801. }
  802.  
  803. notest3d(view,screen,window)
  804. struct View *view;
  805. struct Screen *screen;
  806. struct Window *window;
  807. {
  808.     int tickcount = 0;
  809.     int error = 0;
  810.     WORD actiontoggle = OFF;
  811.     struct IntuiMessage *message;
  812.     WORD mousex, mousey;
  813.     WORD i, j, k;
  814.     WORD mousetoggle = UP ;
  815.     WORD messagenum = 0;
  816.     ULONG framecount = 0;
  817.  
  818.     /* now wait for instructions from the user */
  819.  
  820.     message = (struct IntuiMessage *)GetMsg(window->UserPort);
  821.  
  822.     while ( (!message) || (message->Class) != CLOSEWINDOW )
  823.     {
  824.    if(message) ReplyMsg(message);
  825.    message = (struct IntuiMessage *)GetMsg(window->UserPort);
  826.     }
  827.     ReplyMsg(message);
  828.  
  829.     return(0);
  830.  
  831. }
  832.  
  833.  
  834. donothing(view,screen,window)
  835. struct View *view;
  836. struct Screen *screen;
  837. struct Window *window;
  838. {
  839.     int tickcount = 0;
  840.     int error = 0;
  841.     WORD actiontoggle = OFF;
  842.     struct IntuiMessage *message;
  843.     WORD mousex, mousey;
  844.     WORD i, j, k;
  845.     WORD mousetoggle = UP ;
  846.     WORD messagenum = 0;
  847.     ULONG framecount = 0;
  848.  
  849.     /* now wait for instructions from the user */
  850.  
  851.     message = (struct IntuiMessage *)GetMsg(window->UserPort);
  852.  
  853.     while ( (!message) || (message->Class) != CLOSEWINDOW )
  854.     {
  855.    if(message) ReplyMsg(message);
  856.  
  857.    message = (struct IntuiMessage *)GetMsg(window->UserPort);
  858.     }
  859.     ReplyMsg(message);
  860.  
  861.     return(0);
  862.  
  863. }
  864.  
  865. /**********************************************************************************/
  866.  
  867. main()
  868. {
  869.     int (*usrproc)();
  870.     WORD left = LEFT;
  871.     WORD top = IS_PAL? PAL_TOP : NTSC_TOP ;
  872.     WORD width = WIDTH;
  873.     WORD height = IS_PAL? PAL_HEIGHT : NTSC_HEIGHT ;
  874.     WORD n_bit_planes = N_BIT_PLANES;
  875.     WORD i, j;
  876.     WORD temp1, temp2;
  877.     int error = FALSE;
  878.  
  879.     usrproc = test3d;
  880.  
  881.     /* open the dos library */
  882.  
  883.     if ( (DosBase = OpenLibrary("dos.library",0) ) == NULL)
  884.     {
  885. #ifdef DEBUG
  886.    printf("main: can't open dos lib...\n");
  887. #endif
  888.    exit(-1);
  889.     }
  890.  
  891.     /* load the object data segment */
  892.  
  893. #ifdef GREENHILLS
  894.  
  895.     if ( (objectsegment = LoadSeg("3dobject") ) == NULL)
  896.     {
  897. #ifdef DEBUG
  898.    printf("main: can't load objectsegment...\n");
  899. #endif
  900.    exit(0);
  901.     }
  902.     else
  903.     {
  904.    Amiga = (struct Object *)BADDR( (objectsegment+1) );
  905.     }
  906.  
  907. #else
  908.  
  909.    Amiga = UniverseObject;
  910.  
  911. #endif
  912.  
  913.     /* open the graphics library */
  914.  
  915.     if ( (GfxBase = OpenLibrary("graphics.library",0) ) == NULL)
  916.     {
  917. #ifdef DEBUG
  918.    printf("main: can't open graphics lib...\n");
  919. #endif
  920.    exit(-1);
  921.     }
  922.  
  923.     /* open a window in a custom screen under dos */
  924.  
  925.     /* first, allocate a temporary raster for request routine to use */
  926.  
  927. #ifdef DEBUG
  928.    printf("main: allocate a temporary raster...\n");
  929. #endif
  930.  
  931.     if ((tmpras.RasPtr = (BYTE *)AllocRaster(TMPWIDTH,TMPHEIGHT)) == NULL)
  932.     {
  933. #ifdef DEBUG
  934.    printf("main: can't allocate tmpras memory...\n");
  935. #endif
  936.    error = TRUE;
  937.     }   
  938.     else
  939.     {
  940.    tmpras.Size = RASSIZE(TMPWIDTH,TMPHEIGHT);
  941.     }
  942.  
  943.     /* now set up a bitmap for 3d routine to use */
  944.  
  945. #ifdef DEBUG
  946.    printf("main: set up a super-bit map...\n");
  947. #endif
  948.     
  949.     bitmap0.BytesPerRow = ( ((width+15)>>3) & 0xFFFE);
  950.     bitmap0.Rows = ((height>(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE))?(IS_PAL?PAL_MAXHILACE:NTSC_MAXHILACE):(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE));
  951.     bitmap0.Flags = 0;
  952.     bitmap0.Depth = n_bit_planes;
  953.    
  954.     for (i=0; i< n_bit_planes; i++)
  955.     {
  956.    if(!error)
  957.    {
  958.        temp1 = (IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE);
  959.        temp2 = (IS_PAL?PAL_MAXHILACE:NTSC_MAXHILACE);
  960.        if ( (bitmap0.Planes[i] = 
  961.     (PLANEPTR)AllocMem(RASSIZE(width,(height>temp1)?temp2:temp1),
  962.     MEMF_CHIP|MEMF_CLEAR) ) == NULL)
  963.        {
  964. #ifdef DEBUG
  965.       printf("main: can't allocate bitmap memory...\n");
  966. #endif
  967.       error = TRUE;
  968.       for (--i; i >= 0; --i) FreeRaster(bitmap0.Planes[i],width,((height>(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE))?(IS_PAL?PAL_MAXHILACE:NTSC_MAXHILACE):(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE)));
  969.         
  970.        }
  971.        else
  972.        {
  973. #ifdef DEBUG
  974.       printf("main: allocated  bitmap0.Planes[%lx] = %lx\n",i,bitmap0.Planes[i]);
  975. #endif
  976.        }
  977.    }
  978.     }
  979.  
  980.     /* now set up an alternate bitmap for 3d routine to use for double buffering */
  981.  
  982. #ifdef DEBUG
  983.    printf("main: set up an alternate bitmap...\n");
  984. #endif
  985.     
  986.     bitmap1.BytesPerRow = ( ((width+15)>>3) & 0xFFFE);
  987.     bitmap1.Rows = ((height>(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE))?(IS_PAL?PAL_MAXHILACE:NTSC_MAXHILACE):(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE));
  988.     bitmap1.Flags = 0;
  989.     bitmap1.Depth = n_bit_planes;
  990.    
  991.     for (i=0; i< n_bit_planes; i++)
  992.     {
  993.    if(!error)
  994.    {
  995.        temp1 = (IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE);
  996.        temp2 = (IS_PAL?PAL_MAXHILACE:NTSC_MAXHILACE);
  997.        if ( (bitmap1.Planes[i] = 
  998.     (PLANEPTR)AllocMem(RASSIZE(width,(height>temp1)?temp2:temp1),
  999.     MEMF_CHIP|MEMF_CLEAR) ) == NULL)
  1000.        {
  1001. #ifdef DEBUG
  1002.       printf("main: can't allocate bitmap memory...\n");
  1003. #endif
  1004.       error = TRUE;
  1005.       for (--i; i >= 0; --i) FreeRaster(bitmap1.Planes[i],width,((height>(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE))?(IS_PAL?PAL_MAXHILACE:NTSC_MAXHILACE):(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE)));
  1006.        }
  1007.        else
  1008.        {
  1009. #ifdef DEBUG
  1010.       printf("main: allocated  bitmap1.Planes[%lx] = %lx\n",i,bitmap1.Planes[i]);
  1011. #endif
  1012.        }
  1013.    }
  1014.     }
  1015.  
  1016. #ifdef DEBUG
  1017.     printf("main: call startgfxdos()...\n");
  1018. #endif
  1019.  
  1020.     if(!error)
  1021.     {
  1022.    error = startgfxdos(left,top,height,width,n_bit_planes,usrproc,title,SIMPLE_REFRESH|BACKDROP|ACTIVATE,&tmpras,NULL);
  1023.     }    
  1024.  
  1025. #ifdef ODEBUG
  1026.     printf("returned from startgfxdos with error = %lx\n",error);
  1027. #endif
  1028.  
  1029.     if(!error)
  1030.     {
  1031. #ifdef ODEBUG
  1032.    printf("main: clean up...\n");
  1033. #endif
  1034.  
  1035. #ifdef ODEBUG
  1036.    printf("main: free tempras memory...\n");
  1037. #endif
  1038.    FreeRaster(tmpras.RasPtr,TMPWIDTH,TMPHEIGHT);
  1039.  
  1040. #ifdef ODEBUG
  1041.    printf("main: free bitmap0 memory...\n");
  1042. #endif
  1043.    for (i=0; i< n_bit_planes; i++)
  1044.    {
  1045. #ifdef ODEBUG
  1046.        printf("main: freeing bitmap0.Plane[%lx] = %lx\n",i,bitmap0.Planes[i]);
  1047. #endif
  1048.        FreeRaster(bitmap0.Planes[i],width,((height>(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE))?(IS_PAL?PAL_MAXHILACE:NTSC_MAXHILACE):(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE)));
  1049.    }
  1050.  
  1051. #ifdef ODEBUG
  1052.    printf("main: free bitmap1 memory...\n");
  1053. #endif
  1054.    for (i=0; i< n_bit_planes; i++)
  1055.    {
  1056. #ifdef ODEBUG
  1057.        printf("main: freeing bitmap1.Plane[%lx] = %lx\n",i,bitmap1.Planes[i]);
  1058. #endif
  1059.        FreeRaster(bitmap1.Planes[i],width,((height>(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE))?(IS_PAL?PAL_MAXHILACE:NTSC_MAXHILACE):(IS_PAL?PAL_MAXHINOLACE:NTSC_MAXHINOLACE)));
  1060.    }
  1061.  
  1062.     }
  1063.  
  1064.     /* unload the objectsegment before exiting */
  1065.  
  1066. #ifdef GREENHILLS
  1067.  
  1068. #ifdef ODEBUG
  1069.     printf("main: unload the objectsegment before exiting...\n");
  1070. #endif
  1071.  
  1072.     UnLoadSeg(objectsegment);
  1073.  
  1074. #endif
  1075.  
  1076.     /* exit main */
  1077.  
  1078. #ifdef ODEBUG
  1079.     printf("main: exit main...\n");
  1080. #endif
  1081.  
  1082.  
  1083. }
  1084.  
  1085.  
  1086.  
  1087.